package com.ruoyi.delivery.service.impl;

import com.ruoyi.delivery.domain.DelOperatorTransportDetail;
import com.ruoyi.delivery.domain.DelOperatorTransportOrder;
import com.ruoyi.delivery.mapper.DelOperatorTransportDetailMapper;
import com.ruoyi.delivery.mapper.DelOperatorTransportOrderMapper;
import com.ruoyi.delivery.service.IDelOperatorTransportOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
/**
 * 运输订单 服务层
 *
 * @author hu
 */
@Service
public class DelOperatorTransportOrderImpl implements IDelOperatorTransportOrderService {


    @Autowired
    private DelOperatorTransportOrderMapper OperatorTransportOrderMapper;
    @Autowired
    private DelOperatorTransportDetailMapper operatorTransportDetailMapper;

    @Override
    public List<DelOperatorTransportOrder> filterOperatorTransportOrders(
            String logisticId,
            String trackingNumber,
            String origin,
            String destination,
            String departStartDate,
            String departEndDate,
            String arriveStartDate,
            String arriveEndDate,
            String state,
            String customerType,
            int page,
            int pageSize
    ) {
        // 计算分页的起始偏移量
        int offset = (page - 1) * pageSize;
        return OperatorTransportOrderMapper.filterOperatorTransportOrders(
                logisticId,
                trackingNumber,
                origin,
                destination,
                departStartDate,
                departEndDate,
                arriveStartDate,
                arriveEndDate,
                state,
                customerType,
                offset,
                pageSize
        );
    }

    @Override
    public int getOperatorTransportOrderCount(
            String logisticId,
            String trackingNumber,
            String origin,
            String destination,
            String departStartDate,
            String departEndDate,
            String arriveStartDate,
            String arriveEndDate,
            String state,
            String customerType
    ) {
        return OperatorTransportOrderMapper.getOperatorTransportOrderCount(
                logisticId,
                trackingNumber,
                origin,
                destination,
                departStartDate,
                departEndDate,
                arriveStartDate,
                arriveEndDate,
                state,
                customerType
        );
    }

    @Override
    public boolean insertOperatorTransportOrder(DelOperatorTransportOrder order) {
        return OperatorTransportOrderMapper.insertOperatorTransportOrder(order) > 0;
    }

    @Override
    public boolean deleteOperatorTransportOrder(String trackingNumber) {
        return OperatorTransportOrderMapper.deleteOperatorTransportOrder(trackingNumber) > 0;
    }

    @Override
    public boolean updateOperatorTransportOrder(DelOperatorTransportOrder order) {
        return OperatorTransportOrderMapper.updateOperatorTransportOrder(order) > 0;
    }

    @Override
    public List<DelOperatorTransportDetail> getDetailsByTrackingNumber(String trackingNumber) {
        return operatorTransportDetailMapper.getDetailsByTrackingNumber(trackingNumber);
    }

    // 更新时间为当前时间
    @Override
    public int confirmOperatorTransportOrder(String trackingNumber, String updatedPerson) {
        return OperatorTransportOrderMapper.updateOperatorTransportOrderState(trackingNumber, "待配送",updatedPerson);
    }
    /**
     * 修改预期到货时间
     * @param trackingNumber 运单号
     * @param departureDate 新的到货时间
     * @param updatedPerson 修改人
     */
    public void changeExpectedArrivalTime(String trackingNumber, String departureDate, String updatedPerson) {
        OperatorTransportOrderMapper.updateExpectedArrivalTime(trackingNumber, departureDate, updatedPerson);
    }
}
